cmake_minimum_required(VERSION 3.8)
project(robot_control)

# 编译器警告设置
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# 查找依赖包
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(std_srvs REQUIRED)
find_package(action_msgs REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(rcl_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)

# 自定义接口定义
set(msg_files
  "msg/LidarData.msg"
)

set(srv_files
  "srv/RobotStateSrv.srv"
)

set(action_files
  "action/MoveAction.action"
)

# 生成接口代码
rosidl_generate_interfaces(${PROJECT_NAME}
  ${msg_files}
  ${srv_files}
  ${action_files}
  DEPENDENCIES std_msgs geometry_msgs action_msgs
)

# 编译所有节点
add_executable(lidar_publisher src/lidar_publisher.cpp)
ament_target_dependencies(lidar_publisher 
  rclcpp 
  std_msgs
  sensor_msgs
  rosidl_default_generators
)

add_executable(obstacle_detector src/obstacle_detector.cpp)
ament_target_dependencies(obstacle_detector 
  rclcpp 
  std_msgs
  sensor_msgs
  rosidl_default_generators
)

add_executable(state_service src/state_service.cpp)
ament_target_dependencies(state_service 
  rclcpp 
  std_msgs 
  std_srvs 
  rcl_interfaces
  rosidl_default_generators
)

add_executable(control_client src/control_client.cpp)
ament_target_dependencies(control_client 
  rclcpp 
  std_msgs 
  std_srvs
  rosidl_default_generators
)

add_executable(move_action_server src/move_action_server.cpp)
ament_target_dependencies(move_action_server 
  rclcpp 
  rclcpp_action 
  std_msgs 
  geometry_msgs
  rosidl_default_generators
)

add_executable(move_action_client src/move_action_client.cpp)
ament_target_dependencies(move_action_client 
  rclcpp 
  rclcpp_action 
  std_msgs 
  geometry_msgs
  rosidl_default_generators
)

# 链接自定义接口到所有节点（必须在所有 add_executable 之后）
rosidl_get_typesupport_target(cpp_typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")

target_link_libraries(lidar_publisher ${cpp_typesupport_target})
target_link_libraries(obstacle_detector ${cpp_typesupport_target})
target_link_libraries(state_service ${cpp_typesupport_target})
target_link_libraries(control_client ${cpp_typesupport_target})
target_link_libraries(move_action_server ${cpp_typesupport_target})
target_link_libraries(move_action_client ${cpp_typesupport_target})

# 安装目标到指定目录
install(TARGETS
  lidar_publisher
  obstacle_detector
  state_service
  control_client
  move_action_server
  move_action_client
  DESTINATION lib/${PROJECT_NAME}
)

# 安装接口文件
install(
  DIRECTORY msg/
  DESTINATION share/${PROJECT_NAME}/msg
)

install(
  DIRECTORY srv/
  DESTINATION share/${PROJECT_NAME}/srv
)

install(
  DIRECTORY action/
  DESTINATION share/${PROJECT_NAME}/action
)

install(
  DIRECTORY launch
  DESTINATION share/${PROJECT_NAME}/
)

ament_package()